<?php

abstract class Pageley_Reflection_WrappingInvocationHandler implements Pageley_Reflection_InvocationHandler {
    
    private $wrappedObject;
    
    public function __construct($wrappedObject) {
        $this->wrappedObject = $wrappedObject;
    }
    
    public function invoke($proxy, $methodName, $parameters) {
        $this->beforeInvocation($proxy, $methodName, $parameters);
        $result = null;
        try {
            $result = Pageley_Util_Reflection::invokeMethod($this->wrappedObject, $methodName);
        } catch(Exception $e) {
            $this->onException($proxy, $methodName, $parameters, $e);
            throw $e;
        }
        $this->afterInvocation($proxy, $methodName, $parameters, $result);
        return $result;
    }
    
    protected function beforeInvocation($proxy, $methodName, $parameters) {
    }
    
    protected function onException($proxy, $methodName, $parameters, $exception) {
    }
    
    protected function afterInvocation($proxy, $methodName, $parameters, $returnValue) {
    }
}